package cn.wdm.ymcc.web.controller;

import cn.wdm.ymcc.domain.VerifycodeDto;
import cn.wdm.ymcc.exception.GloablException;
import cn.wdm.ymcc.result.JSONResult;
import cn.wdm.ymcc.service.IVerifyCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class MessageImgcodeController {
    @Autowired
    private IVerifyCodeService iVerifyCodeService;
    @GetMapping("/verifycode/imageCode/{imageCodeKey}")
    public JSONResult image(@PathVariable("imageCodeKey") String key){
        try {
            //将前端传的uuid传过去，返回一个base64编码的字符串。图片转为字符串。
            String imageCode=iVerifyCodeService.verifyCode(key);
            return JSONResult.success(imageCode);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.error("获取失败","40040");
        }
    }
    /**
     * 获取短信验证码接口
     * @return
     */
    @PostMapping("/verifycode/sendSmsCode")
    public JSONResult sendSms(@RequestBody VerifycodeDto dto){
        try {
            //获取短信验证码
            iVerifyCodeService.getSmsCode(dto);
            return JSONResult.success();
        } catch (GloablException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return JSONResult.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JSONResult.error(e.getMessage());
        }
    }

}
